package com.example.textspannabletest;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

	private TextView txt_content;
	private SpannableString spt;
	
	private Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initView();
	}

	private void initView() {
		txt_content = (TextView) findViewById(R.id.txt_content);
		btn_1 = (Button) findViewById(R.id.btn_1);
		btn_2 = (Button) findViewById(R.id.btn_2);
		btn_3 = (Button) findViewById(R.id.btn_3);
		btn_4 = (Button) findViewById(R.id.btn_4);
		btn_5 = (Button) findViewById(R.id.btn_5);
		btn_6 = (Button) findViewById(R.id.btn_6);
		btn_7 = (Button) findViewById(R.id.btn_7);
		
		btn_1.setOnClickListener(this);
		btn_2.setOnClickListener(this);
		btn_3.setOnClickListener(this);
		btn_4.setOnClickListener(this);
		btn_5.setOnClickListener(this);
		btn_6.setOnClickListener(this);
		btn_7.setOnClickListener(this);
		
		spt = new SpannableString("这是一个测试:");
		txt_content.setText(spt);
		txt_content.setMovementMethod(LinkMovementMethod.getInstance());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_1:
			addUrlSpan();
			break;
			
		case R.id.btn_2:
			addBackColorSpan();
			break;

		case R.id.btn_3:
			addForeColorSpan();
			break;
			
		case R.id.btn_4:
			addFontSpan();
			break;
			
		case R.id.btn_5:
			addStyleSpan();
			break;
			
		case R.id.btn_6:
			addUnderLineSpan();
			break;
			
		case R.id.btn_7:
			addImageSpan();
			break;
			
		default:
			break;
		}
	}
	
	/**
	 * 超链接
	 */
	public void addUrlSpan(){
		SpannableString spanString = new SpannableString("超链接");
		URLSpan urlSapn = new URLSpan("http://baidu.com");
		spanString.setSpan(urlSapn, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		txt_content.append(spanString);
	}
	
	/**
	 * 文字背景颜色
	 */
	public void addBackColorSpan(){
		SpannableString spanString = new SpannableString("背景颜色");
		BackgroundColorSpan colorSpan = new BackgroundColorSpan(Color.YELLOW);
		spanString.setSpan(colorSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		txt_content.append(spanString);
	}
	
	/**
	 * 文字颜色
	 */
	public void addForeColorSpan(){
		SpannableString spanString = new SpannableString("文字颜色");
		ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
		spanString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		txt_content.append(spanString);
	}
	
	/**
	 * 文字字体大小
	 */
	public void addFontSpan(){
		SpannableString spanString = new SpannableString("字体大小");
		AbsoluteSizeSpan span = new AbsoluteSizeSpan(60);
		spanString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		txt_content.append(spanString);
	}
	
	/**
	 * 文字的风格
	 */
	public void addStyleSpan(){
		SpannableString spanString = new SpannableString("文字风格");
		StyleSpan span = new StyleSpan(Typeface.BOLD);
		spanString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		txt_content.append(spanString);
	}
	
	/**
	 * 设置删除线
	 */
	public void addUnderLineSpan(){
		SpannableString spanString = new SpannableString("删除线");
		StrikethroughSpan span = new StrikethroughSpan();
		spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		txt_content.append(spanString);
	}
	
	/**
	 * 图片
	 */
	public void addImageSpan(){
		SpannableString spanString = new SpannableString(" ");
		Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
		d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
		ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
		spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		txt_content.append(spanString);
	}
	

}
